package com.example.library.fragment;

import android.media.MediaPlayer;
import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.text.Html;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;

import com.example.library.R;
import com.example.library.myRecyclerview.AttributeDecoration;
import com.example.library.myRecyclerview.CommonAdapter;
import com.example.library.myRecyclerview.base.ViewHolder;
import com.example.library.player.MyMediaPlayer;
import com.example.library.http.KnowledgeParseEntity.CxCollectionBean.MeanCollectionBean.SenCollectionBean;

import java.util.ArrayList;
import java.util.List;

import io.reactivex.annotations.Nullable;

/**
 * Created by dadong on 2018/9/19.
 * Describe:
 */
public class NewKnowledgeParseFragment extends Fragment {

    View mView;
    List<SenCollectionBean> paraphraseBeans = new ArrayList<>();
    RecyclerView rvUsage;
    CommonAdapter<SenCollectionBean> paraphraseAdapter;
    MyMediaPlayer player;
    LinearLayoutManager rvUsageLm;

    @Nullable
    @Override
    public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
        mView = inflater.inflate(R.layout.view_knowledge_useage, container, false);
        initView(mView);
        return mView;
    }

    private void initView(View view) {
        rvUsageLm = new LinearLayoutManager(getContext());
        rvUsageLm = new LinearLayoutManager(getContext()) {
            @Override
            public RecyclerView.LayoutParams generateDefaultLayoutParams() {
                return new RecyclerView.LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT,
                        ViewGroup.LayoutParams.WRAP_CONTENT);
            }
        };
        player = new  MyMediaPlayer();
        rvUsage = view.findViewById(R.id.rv_usage);
        if (paraphraseBeans.size() > 0) {
            initParaphraseAdapter();
            rvUsage.setAdapter(paraphraseAdapter);
        }
        rvUsage.setLayoutManager(rvUsageLm);
        rvUsage.addItemDecoration(new AttributeDecoration(getContext(), new AttributeDecoration.DecorationCallback() {
            @Override
            public String getGroupId(int position) {
                return paraphraseBeans.get(position).getCxEnglish();
            }

            @Override
            public String getGroupFirstLine(int position) {
                return paraphraseBeans.get(position).getCxEnglish();
            }
        }));
    }

    private void initParaphraseAdapter() {
        paraphraseAdapter = new CommonAdapter<SenCollectionBean>(getContext(), R.layout.view_example_item, paraphraseBeans) {
            @Override
            protected void convert(ViewHolder holder, final SenCollectionBean senCollectionBean, int position) {
                holder.setText(R.id.tv_english, Html.fromHtml(senCollectionBean.getSentenceEn()) + "");
                holder.setText(R.id.tv_chinese, Html.fromHtml(senCollectionBean.getSTranslation()) + "");
                holder.getView(R.id.cl_play_video).setOnClickListener(new View.OnClickListener() {
                    @Override
                    public void onClick(final View v) {
                        v.setClickable(false);
                        player.setPreparedListener(new MediaPlayer.OnPreparedListener() {
                            @Override
                            public void onPrepared(MediaPlayer MediaPlayer) {
                                player.start();
                            }
                        });

                        player.setCompletionListener(new MediaPlayer.OnCompletionListener() {
                            @Override
                            public void onCompletion(MediaPlayer MediaPlayer) {
                                MediaPlayer.reset();
                                v.setClickable(true);
                            }
                        });
//                            player.setDataSource("http://mpge.5nd.com/2015/2015-11-26/69708/1.mp3");
                        player.setDataSource(senCollectionBean.getSViocePath());
                    }
                });
            }
        };
    }

    public List<SenCollectionBean> getParaphraseBeans() {
        return paraphraseBeans;
    }

    public void setParaphraseBeans(List<SenCollectionBean> paraphraseBeans) {
        if (this.paraphraseBeans.size() > 0) {
            this.paraphraseBeans.clear();
        }
        this.paraphraseBeans.addAll(paraphraseBeans);
    }
}
